﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Portal.ViewModels;
using Wicresoft.UnifyShow.Portal.Models;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class AdminPermissionController : BaseController
    {
        private IUser_X user;
        private ISiteBranch siteBranch;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user).Dispose();
            ((IDisposable)siteBranch).Dispose();
        }

        [InjectionConstructor]
        public AdminPermissionController(IUser_X user, ISiteBranch siteBranch)
        {
            this.user = user;
            this.siteBranch = siteBranch;
        }


        //
        // GET: /AdminPermission/

        public ActionResult Index(int page = 1, string sort = "DisplayName", string sortDir = "Ascending", string name = "", string displayname = "")
        {
            base.SetRoles(user);
            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            const int pageSize = 10;

            Decimal totalRecords;
            IEnumerable<User_XModel> users = user.GetListPageAndSortUser_X(out totalRecords, pageSize, page, sort, sortDir, name, displayname);
            PagedUser_XsModel model = new PagedUser_XsModel
            {
                PageSize = pageSize,
                PageNumber = page,
                User_Xs = users,
                TotalRows = totalRecords
            };
            return View(model);
        }


        public ActionResult UserInfo(int userID)
        {
            PermissionModel m = new PermissionModel();
            SFUser_XModel u = user.GetUserByIDSF(userID);
            m.UserID = int.Parse(u.ID.ToString());
            m.LoginID = u.Name;
            m.IsConfigAdmin = u.IsAdmin == 1 ? true : false;
            m.IsLeader = u.IsLeader == 1 ? true : false;
            m.IsAccessUS = u.AccessUS == 1 ? true : false;
            m.IsAccessITSM = u.AccessITSM == 1 ? true : false;
            m.IsAccessGJ = u.AccessGJ == 1 ? true : false;
            m.UserName = u.DisplayName;
            m.UserJob = u.Position;
            m.UserDepartment = u.Department;
            m.UserSite = u.Company;
            m.Mobile = u.Mobile;
            m.Email = u.Email;

            //查询登录用户的在统一展现系统所有有权限的地市
            List<SiteBranchModel> listSites = siteBranch.GetSiteBranchByUserID(userID);
            //查询登录用户的在告警系统所有有权限的地市
            List<SiteBranchModel> listSites_gj = siteBranch.GetSiteBranch_GJByUserID(userID);
            //查询所有的地市
            List<SiteBranchModel> allSites = siteBranch.GetSiteALLBranch().Where(o => !string.IsNullOrWhiteSpace(o.Alias)).ToList();
            //给用户在统一展现系统和告警系统的地市权限赋值
            List<US_SiteAccessModel> us_SiteAccessList = new List<US_SiteAccessModel>();
            List<GJ_SiteAccessModel> gj_SiteAccessList = new List<GJ_SiteAccessModel>();
            foreach (var item in allSites)
            {
                US_SiteAccessModel usmodel = new US_SiteAccessModel();
                usmodel.ID = item.ID;
                usmodel.Alias = item.Alias;
                usmodel.IsAccess = listSites.Where(o => o.ID == item.ID).Count() >= 1;
                us_SiteAccessList.Add(usmodel);
                GJ_SiteAccessModel gjmodel = new GJ_SiteAccessModel();
                gjmodel.ID = item.ID;
                gjmodel.Alias = item.Alias;
                gjmodel.IsAccess = listSites_gj.Where(o => o.ID == item.ID).Count() >= 1;
                gj_SiteAccessList.Add(gjmodel);
            }
            m.US_SiteAccessList = us_SiteAccessList;
            m.GJ_SiteAccessList = gj_SiteAccessList;




            return PartialView(m);
        }

        public ActionResult UserEdit(int userID)
        {
            return PartialView(((PartialViewResult)UserInfo(userID)).Model);
        }

        [HttpPost]
        public ActionResult UserEdit(PermissionModel model)
        {

            List<decimal> roles_us = new List<decimal>();
            List<decimal> roles_gj = new List<decimal>();
            List<US_SiteAccessModel> us_SiteAccessList = model.US_SiteAccessList;
            List<GJ_SiteAccessModel> gj_SiteAccessList = model.GJ_SiteAccessList;
            foreach (var item in us_SiteAccessList)
            {
                if (item.IsAccess)
                {
                    roles_us.Add(item.ID);
                }
            }

            foreach (var item in gj_SiteAccessList)
            {
                if (item.IsAccess)
                {
                    roles_gj.Add(item.ID);
                }
            }

            var sf = new SFUser_XModel();

            sf.ID = model.UserID;
            sf.IsAdmin = model.IsConfigAdmin == true ? 1 : 0;
            sf.IsLeader = model.IsLeader == true ? 1 : 0;
            sf.AccessUS = model.IsAccessUS == true ? 1 : 0;
            sf.AccessITSM = model.IsAccessITSM == true ? 1 : 0;
            sf.AccessGJ = model.IsAccessGJ == true ? 1 : 0;
            sf.Name = model.LoginID;

            sf.Department = model.UserDepartment;
            sf.Position = model.UserJob;
            sf.Mobile = model.Mobile;
            sf.Email = model.Email;
            sf.Disabled = model.Disabled ? 1 : 0;

            return Json(user.UpdateRoleByUserID(sf, roles_us, roles_gj));
            //return PartialView(model);
        }

        public ActionResult ChangePassword()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult ChangePassword(string login, string pass)
        {
            return Json(user.ChangePassword(login, pass));
        }

        public ActionResult CreateUser()
        {
            base.SetRoles(user);
            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            var x = new SFUser_XModel();
            ViewBag.Post = false;
            ViewData["Sites"] = siteBranch.GetSiteALLBranch().Select(o => o.Name).ToList();
            return View(x);
        }

        [HttpPost]
        public ActionResult CreateUser(SFUser_XModel model)
        {
            string resultStr = string.Empty;
            ViewBag.Post = true;
            ViewBag.Status = user.Create(model,out resultStr);
            ViewBag.ResultStr = resultStr;
            ViewData["Sites"] = siteBranch.GetSiteALLBranch().Select(o => o.Name).ToList();
            return View(model);
        }

        [HttpPost]
        public ActionResult Exist(string name)
        {
            return Json(user.Exist(name));
        }
    }
}
